function sensaris_generate_figures(mat_directory, output_directory)
% SENSARIS - Process log files from log_directory and generates
%   *.mat files in output_directory.
%
%   A/YYMMDD.LOG  =>  YYMMDD_A.mat
%
% Usage:
%   values=parse_logfile('100129.A.LOG');
%

%% cleaning

%% global variables
global BASE_DIRECTORY;
global NOX_SCALING;
global COX_SCALING;
global HUM_SCALING;
global TEMP_SCALING;
global NOISE_SCALING;
global BATT_SCALING;
global DEVICE_IDS;


files = dir(strcat(mat_directory, '/*.mat'));
nfiles = size(files,1);
for j=1:nfiles
    filename = files(j).name;
    file_fullname = strcat(mat_directory,'/', filename);
    load(file_fullname);
    disp(['Loaded:', file_fullname]);

    h = figure ('Name', ['Sensaris: ', filename] );
    hold on;
    ylabel('Normalized scale');
    legend_names = {};
    legend_names = plot_sensor_data(values, legend_names);

    legend (legend_names); %legend should be added after plots
    datetick('x','HH:MM','keepticks')

    % saveas
    jpg_file_fullname = strcat(filename,'.jpg');
    saveas(h, jpg_file_fullname  ,'jpg');
end






function legend_names = plot_sensor_data(values, legend_names)

%% global variables
global BASE_DIRECTORY;
global NOX_SCALING;
global COX_SCALING;
global HUM_SCALING;
global TEMP_SCALING;
global NOISE_SCALING;
global BATT_SCALING;
global DEVICE_IDS;

sensorData = [values.data(:,2)*TEMP_SCALING values.data(:,3)*HUM_SCALING ...
    values.data(:,4)*NOX_SCALING values.data(:,5)*COX_SCALING ...
    values.data(:,8)*BATT_SCALING values.data(:,9)*NOISE_SCALING ];

color = get_a_new_color();
plot(values.data(:,1), sensorData(:,1), 'color',color);
legend_names = [ legend_names, {'T'} ];

color = get_a_new_color();
plot(values.data(:,1), sensorData(:,2), '-', 'color',color);
legend_names = [ legend_names, {'H'} ];

color = get_a_new_color();
plot(values.data(:,1), sensorData(:,3), '-', 'color',color);
legend_names = [ legend_names, {'NOx'} ];

color = get_a_new_color();
plot(values.data(:,1), sensorData(:,4), 'color',color);
legend_names = [ legend_names, {'COx'} ];

color = get_a_new_color();
plot(values.data(:,1), sensorData(:,5), 'color',color);
legend_names = [ legend_names, {'Battery'} ];

% color = get_a_new_color();
% plot(values.data(:,1), sensorData(:,6), 'color',color);
